<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Reads a maximum of length bytes from a socket</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.socket-read" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_read</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">socket_read</span> &mdash; <span class="dc-title">Reads a maximum of length bytes from a socket</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-read-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>socket_read</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$type</code><span class="initializer"> = PHP_BINARY_READ</span></span>
  ] ) : <span class="type">string</span></div>

  <p class="para rdfs-comment">
   The function <span class="function"><strong>socket_read()</strong></span> reads from the socket
   resource <code class="parameter">socket</code> created by the
   <span class="function"><a href="socket_create.html" class="function">socket_create()</a></span> or
   <span class="function"><a href="socket_accept.html" class="function">socket_accept()</a></span> functions. 
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-read-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">socket</code></dt>

     <dd>

      <p class="para">
       A valid socket resource created with <span class="function"><a href="socket_create.html" class="function">socket_create()</a></span>
       or <span class="function"><a href="socket_accept.html" class="function">socket_accept()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       The maximum number of bytes read is specified by the
       <code class="parameter">length</code> parameter. Otherwise you can use
       <strong><code>\r</code></strong>, <strong><code>\n</code></strong>,
       or <strong><code>\0</code></strong> to end reading (depending on the <code class="parameter">type</code>
       parameter, see below).
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">type</code></dt>

     <dd>

      <p class="para">
       Optional <code class="parameter">type</code> parameter is a named constant:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara">
          <strong><code>PHP_BINARY_READ</code></strong> (Default) - use the system
          <em>recv()</em> function. Safe for reading binary data.
         </span>
        </li>
        <li class="listitem">
         <span class="simpara">
          <strong><code>PHP_NORMAL_READ</code></strong> - reading stops at
          <em>\n</em> or <em>\r</em>.
         </span>
        </li>
       </ul>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.socket-read-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>socket_read()</strong></span> returns the data as a string on success,
   or <strong><code>FALSE</code></strong> on error (including if the remote host has closed the
   connection). The error code can be retrieved with
   <span class="function"><a href="socket_last_error.html" class="function">socket_last_error()</a></span>. This code may be passed to
   <span class="function"><a href="socket_strerror.html" class="function">socket_strerror()</a></span> to get a textual representation of
   the error.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    <span class="function"><strong>socket_read()</strong></span> returns a zero length string (&quot;&quot;)
    when there is no more data to read.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.socket-read-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="socket_accept.html" class="function" rel="rdfs-seeAlso">socket_accept()</a> - Accepts a connection on a socket</span></li>
    <li class="member"><span class="function"><a href="socket_bind.html" class="function" rel="rdfs-seeAlso">socket_bind()</a> - 给套接字绑定名字</span></li>
    <li class="member"><span class="function"><a href="socket_connect.html" class="function" rel="rdfs-seeAlso">socket_connect()</a> - 开启一个套接字连接</span></li>
    <li class="member"><span class="function"><a href="socket_listen.html" class="function" rel="rdfs-seeAlso">socket_listen()</a> - Listens for a connection on a socket</span></li>
    <li class="member"><span class="function"><a href="socket_last_error.html" class="function" rel="rdfs-seeAlso">socket_last_error()</a> - Returns the last error on the socket</span></li>
    <li class="member"><span class="function"><a href="socket_strerror.html" class="function" rel="rdfs-seeAlso">socket_strerror()</a> - Return a string describing a socket error</span></li>
    <li class="member"><span class="function"><a href="socket_write.html" class="function" rel="rdfs-seeAlso">socket_write()</a> - Write to a socket</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>